package com.bwf.jdbc3;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/6/2 10:10
 * @Copyright 博为峰
 */
public class Test {
    //事务必须是同一个连接
    public static void main(String[] args) {
        Connection conn = null;
        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);//非自动提交
            //插入班级
            ClaDaoImpl claDao = new ClaDaoImpl();
            Cla cla = new Cla(0, "119期", 116);
            int classId = claDao.insert(cla, conn);
            System.out.println("classId = " + classId);
            //插入学生
            StudentDaoImpl studentDao = new StudentDaoImpl();
            Student student = new Student("华为2", "男", new Date(), 1000, new Date(), new Date(), classId);
            studentDao.insert(student, conn);
            conn.commit();//提交
        } catch (SQLException e) {
            try {
                conn.rollback();//回滚
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
